package com.ifinc.common.web.frame;

import com.ifinc.common.db.Pager;
import com.ifinc.common.exception.CheckException;
import com.ifinc.common.utils.ExceptionUtils;
import com.ifinc.common.utils.StringUtils;
import com.ifinc.common.utils.net.HttpUtils;
import com.ifinc.common.utils.net.SocketUtils;
import com.ifinc.common.web.frame.define.Dao;
import com.ifinc.common.web.frame.define.Param;
import com.ifinc.common.web.frame.define.Persistence;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: luotao
 * Date: 13-11-27
 * Time: 14:43
 */
public abstract class BaseObject implements Persistence, Param {

    protected Map<String, String> reqParam = new HashMap<String, String>();

    protected Map<String, String> headerParam = new HashMap<String, String>();

    protected Map<String, Object> sessionParam = new HashMap<String, Object>();

    protected Map<String, Object> resultParam = new HashMap<String, Object>();

    protected Dao dao;

    protected ConfigContext configContext;

    public void setConfigContext(ConfigContext configContext) {
        this.configContext = configContext;
    }

    @Override
    public void setReqParam(Map<String, String> param) {
        this.reqParam = param;
    }

    @Override
    public void setSessionMap(Map<String, Object> sessionMap) {
        this.sessionParam = sessionMap;
    }

    @Override
    public void setHeaderParam(Map<String, String> headerParam) {
        this.headerParam = headerParam;
    }

    @Override
    public Map<String, Object> getResultMap() {
        return this.resultParam;
    }

    @Override
    public Map<String, Object> getSessionMap() {
        return this.sessionParam;
    }

    @Override
    public void setDao(Dao dao) {
        this.dao = dao;
    }

    @Override
    public int save(String sql, Map<String, String> values) {
        return dao.save(sql, values);
    }

    @Override
    public int delete(String sql, Map<String, String> values) {
        return dao.delete(sql, values);
    }

    @Override
    public List list(String sql, Map<String, String> values) {
        return dao.list(sql, values);
    }

    @Override
    public int update(String sql, Map<String, String> values) {
        return dao.update(sql, values);
    }

    @Override
    public Pager listPage(String sqlName, Map<String, String> param, Pager pager) {
        return dao.listPage(sqlName, param, pager);
    }

    @Override
    public Map load(String sql, Map<String, String> map) {
        return dao.load(sql, map);
    }

    public void addResult(String key, Object value) {
        resultParam.put(key, value);
    }

    public abstract void verify(String methodName);

    public void checkLenGBK(String str, int len, String errMsg, Object... value) {
        StringUtils.checkLenGBK(str, len, errMsg, value);
    }

    public void checkLenUTF8(String str, int len, String errMsg, Object... value) {
        StringUtils.checkLenGBK(str, len, errMsg, value);
    }

    public void checkEmpty(String str, String errMsg, Object... value) {
        if (StringUtils.isEmpty(str)) {
            ExceptionUtils.throwEx(new CheckException(errMsg, value));
        }
    }

    public String httpGBK(String url, Map param) {
        byte[] result = HttpUtils.doPost(url, param
                , configContext.getTimeout_conn()
                , configContext.getTimeout_read());
        return StringUtils.encodeStrGBK(result);
    }

    public String httpUTF8(String url, Map param) {
        byte[] result = HttpUtils.doPost(url, param, configContext.getTimeout_conn()
                , configContext.getTimeout_read());
        return StringUtils.encodeStrUTF8(result);
    }

    public String socketGBK(byte[] data, Map param, boolean readLen) {
        try {
            byte[] result = SocketUtils.req(data, configContext.getIp()
                    , configContext.getPort()
                    , configContext.getTimeout_conn()
                    , configContext.getTimeout_read(), readLen);
            return StringUtils.encodeStrGBK(result);
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    }

    public String socketUTF8(byte[] data, Map param, boolean readLen) {
        try {
            byte[] result = SocketUtils.req(data, configContext.getIp()
                    , configContext.getPort()
                    , configContext.getTimeout_conn()
                    , configContext.getTimeout_read(), readLen);
            return StringUtils.encodeStrGBK(result);
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    }


}
